package com.choosefine.it.company.service.Impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.company.dao.CompanyPublishprojectMapper;
import com.choosefine.it.company.model.CompanyPublishproject;
import com.choosefine.it.company.model.vo.CompanyPublishManagerVo;
import com.choosefine.it.company.service.CompanyPublishManageService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 
 * @author chenjian
 * @date 2016-11-30 下午5:13:50
 */
@Service("companyPublishManageService")
public class CompanyPublishManageServiceImpl implements CompanyPublishManageService {
	@Autowired
	private CompanyPublishprojectMapper companyPublishprojectMapper;
	@Autowired 
	MongoTemplate mongoTemplate;
	@Override
	public Page<CompanyPublishManagerVo> findPublishProjectList(PageDTO pageDto, Map<String,Object> params) {
		PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
		Page<CompanyPublishManagerVo> page = companyPublishprojectMapper.findPublishProjectList(params);
		return page;
	}
	@Override
	public CompanyPublishproject findCompanyPublishProjectById(Integer publishId) {
		return companyPublishprojectMapper.findCompanyPublishProjectById(publishId);
	}
	@Override
	public int updateCompanyPublishproject(Map<String, Object> params){
		int num = companyPublishprojectMapper.updateCompanyPublishproject(params);
		if (num > 0) {
			Update update = new Update();
			update.set("publishStatus", params.get("publishStatus"));
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))), update, CompanyPublishproject.class);
		}
		return num;
	}
	@Override
	public int deleteCompanyPublishProject(Map<String, Object> params) {
		int num = companyPublishprojectMapper.deleteCompanyPublishProject(params);
		if (num > 0){
			Update update = new Update();
			update.set("isValid", params.get("isValid"));
			update.set("utime", params.get("utime"));
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))), update, CompanyPublishproject.class);
		}
		return num;
	}
	@Override
	public CompanyPublishproject findCompanyPublishProjectDetailById(
			Integer publishId) {
		return mongoTemplate.findOne(new Query(Criteria.where("_id").is(publishId)), CompanyPublishproject.class);
	}
	
}
